package hk.m4s.lr.repair.test.ui;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import framework.common.Constant;
import framework.common.baseui.UeBaseActivity;
import framework.common.runtimepermissions.PermissionsManager;
import framework.common.runtimepermissions.PermissionsResultAction;
import framework.common.zanetwork.core.ResponseCallback;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.AccountModel;
import hk.m4s.lr.repair.test.model.ProvinceModel;
import hk.m4s.lr.repair.test.service.AccountSerive;
import hk.m4s.lr.repair.test.ui.anount.LoginActivity;
import hk.m4s.lr.repair.test.ui.anount.RegistActivity;
import hk.m4s.lr.repair.test.utils.LocationCallBack;
import hk.m4s.lr.repair.test.utils.LocationUtils;
import hk.m4s.lr.repair.test.utils.ToastUtil;
import hk.m4s.lr.repair.test.views.ShouYeViewPager;
import okhttp3.Response;

public class MainActivity extends UeBaseActivity implements View.OnClickListener{
    private ShouYeViewPager mViewPger;
    private List<Fragment> fragments = new ArrayList<>();
    HomeFragment homeFragment=new HomeFragment();
    NewsFragment newsFragment=new NewsFragment();
    CarShopFragment carShopFragment=new CarShopFragment();
    MyFragment myFragment=new MyFragment();
    private LinearLayout footer1,footer2,footer3,footer4,footer5;
    private ImageView footer1_img,footer2_img,footer3_img,footer4_img;
    private TextView footer1_text,footer2_text,footer3_text,footer4_text;
    private long currTimemill;
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseSetContentView(R.layout.activity_main);
        context=this;
        hiddenGoBackBtn();
        setTitleText("左右维修企业");
        requestPermissions();
        initView();
    }
    public void initView(){
        mViewPger = (ShouYeViewPager) findViewById(R.id.mViewPager);
        fragments.add(homeFragment);
        fragments.add(newsFragment);
        fragments.add(carShopFragment);
        fragments.add(myFragment);
        ViewPagerAdapter mainAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        mViewPger.setAdapter(mainAdapter);
        mViewPger.addOnPageChangeListener(mainAdapter);
        mViewPger.setCurrentItem(0);
        footer1=(LinearLayout)findViewById(R.id.footer1);
        footer2=(LinearLayout)findViewById(R.id.footer2);
        footer3=(LinearLayout)findViewById(R.id.footer3);
        footer4=(LinearLayout)findViewById(R.id.footer4);
        footer5=(LinearLayout)findViewById(R.id.footer5);

        footer1.setOnClickListener(this);
        footer2.setOnClickListener(this);
        footer3.setOnClickListener(this);
        footer4.setOnClickListener(this);
        footer5.setOnClickListener(this);
        footer1_img=(ImageView)findViewById(R.id.footer1_img);
        footer2_img=(ImageView)findViewById(R.id.footer2_img);
        footer3_img=(ImageView)findViewById(R.id.footer3_img);
        footer4_img=(ImageView)findViewById(R.id.footer4_img);
        footer1_text=(TextView)findViewById(R.id.footer1_text);
        footer2_text=(TextView)findViewById(R.id.footer2_text);
        footer3_text=(TextView)findViewById(R.id.footer3_text);
        footer4_text=(TextView)findViewById(R.id.footer4_text);
        pushId();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.footer1:
                  footer1_img.setImageResource(R.mipmap.maintain_hover);
                footer2_img.setImageResource(R.mipmap.industry_refer);
                footer3_img.setImageResource(R.mipmap.maintain_store);
                footer4_img.setImageResource(R.mipmap.user_center);
                footer1_text.setTextColor(Color.parseColor("#108add"));
                footer2_text.setTextColor(Color.parseColor("#777777"));
                footer3_text.setTextColor(Color.parseColor("#777777"));
                footer4_text.setTextColor(Color.parseColor("#777777"));
                switchUI(0);
                break;
            case R.id.footer2:
                footer1_img.setImageResource(R.mipmap.maintain);
                footer2_img.setImageResource(R.mipmap.industry_refer_hover);
                footer3_img.setImageResource(R.mipmap.maintain_store);
                footer4_img.setImageResource(R.mipmap.user_center);
                footer1_text.setTextColor(Color.parseColor("#777777"));
                footer2_text.setTextColor(Color.parseColor("#108add"));
                footer3_text.setTextColor(Color.parseColor("#777777"));
                footer4_text.setTextColor(Color.parseColor("#777777"));
                switchUI(1);
                break;
            case R.id.footer4:
                footer1_img.setImageResource(R.mipmap.maintain);
                footer2_img.setImageResource(R.mipmap.industry_refer);
                footer3_img.setImageResource(R.mipmap.maintain_store_hover);
                footer4_img.setImageResource(R.mipmap.user_center);
                footer1_text.setTextColor(Color.parseColor("#777777"));
                footer2_text.setTextColor(Color.parseColor("#777777"));
                footer3_text.setTextColor(Color.parseColor("#108add"));
                footer4_text.setTextColor(Color.parseColor("#777777"));
                switchUI(3);
                break;
            case R.id.footer5:
               footer1_img.setImageResource(R.mipmap.maintain);
                footer2_img.setImageResource(R.mipmap.industry_refer);
                footer3_img.setImageResource(R.mipmap.maintain_store);
                footer4_img.setImageResource(R.mipmap.user_center_hover);
                footer1_text.setTextColor(Color.parseColor("#777777"));
                footer2_text.setTextColor(Color.parseColor("#777777"));
                footer3_text.setTextColor(Color.parseColor("#777777"));
                footer4_text.setTextColor(Color.parseColor("#108add"));
                switchUI(4);
                break;
        }
    }
    private void switchUI(int index){
        hiddenHeader();
        switch (index){
            case 0:
                showHeader();
                hiddenGoBackBtn();
                setTitleText("左右维修企业");
                mViewPger.setCurrentItem(0);
                break;
            case 1:
                showHeader();
                hiddenGoBackBtn();
                setTitleText("行业资讯");
                mViewPger.setCurrentItem(1);
                break;
            case 2:

                break;
            case 3:
                hiddenHeader();
                hiddenGoBackBtn();
                setTitleText("汽保商城");
                mViewPger.setCurrentItem(2);
                break;
            case 4:
                hiddenHeader();
                hiddenGoBackBtn();
                mViewPger.setCurrentItem(3);
                break;
        }
    }
    @TargetApi(23)
    private void requestPermissions() {
        PermissionsManager.getInstance().requestAllManifestPermissionsIfNecessary(this, new PermissionsResultAction() {
            @Override
            public void onGranted() {
//				Toast.makeText(MainActivity.this, "All permissions have been granted", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDenied(String permission) {
                //Toast.makeText(MainActivity.this, "Permission " + permission + " has been denied", Toast.LENGTH_SHORT).show();
            }
        });
    }
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        PermissionsManager.getInstance().notifyPermissionsChange(permissions, grantResults);
    }
    public class ViewPagerAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener{


        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
        }

        @Override
        public void onPageSelected(int position) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            long time = System.currentTimeMillis();
            if ((time - currTimemill) > 1500) {
                ToastUtil.toast(this,"再按一次退出左右-维修");
                currTimemill = time;
            } else {
                this.finish();
            }




            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    public void pushId(){
        Map<String, Object> parmas = new HashMap<>();
        parmas.put("device_type", Constant.device_type);
        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token,""));
        try {
            JSONObject jsondata = new JSONObject();
            jsondata.put("pushId", SharedPreferencesUtils.getSharedPreferences(Constant.pushId,""));
            parmas.put("jsonText", jsondata.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        AccountSerive.putPushId(context,parmas, new ResponseCallback<AccountModel>() {
            @Override
            public void onSuccess(AccountModel accountModel) {

            }

            @Override
            public void onError(Response response, int errorCode, String errorMsg, Exception e) {

            }
        });
    }
}

